package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysLog;
import com.powernode.model.Result;
import com.powernode.service.SysLogService;
import com.powernode.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 系统操作日志管理控制层
 *
 * @author HP
 */
@Api(tags = "系统操作日志接口管理")
@RequestMapping("sys/log")
@RestController
public class SysLogController {
    @Autowired
    SysLogService sysLogService;

    /**
     * 多条件分页查询系统操作日志
     *
     * @param current   页码
     * @param size      每页显示条数
     * @param userId    用户标识
     * @param operation 用户操作描述
     * @return
     */

    @ApiOperation("多条件分页查询系统操作日志")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page<SysLog>> loadSysLogPage(@RequestParam Long current, @RequestParam Long size, @RequestParam(required = false) Long userId, @RequestParam(required = false) String operation) {
        // 创建分页对象并设置当前页和每页大小
        Page<SysLog> page = new Page<>(current, size);
        // 执行分页查询，根据用户ID和操作描述进行条件筛选，并按创建日期降序排序
        page = sysLogService.page(page, new LambdaQueryWrapper<SysLog>()
            .eq(ObjectUtil.isNotNull(userId), SysLog::getUserId, userId)
            .like(StringUtils.hasText(operation), SysLog::getOperation, operation)
            .orderByDesc(SysLog::getCreateDate));
        // 返回查询结果
        return Result.success(page);
    }
}